package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDetailetIDaompl implements OrderDetailetDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
//批量添加订单明细
    @Override
    public int save(List<OrderDetailet> orderdetailet) throws SQLException {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            StringBuffer stringBuffer = new StringBuffer("insert into orderdetailet(orderId,foodId,quantity) values (?,?,?)");
            //如果集合中只有一条数据不拼串
            for (int i = 0;i<orderdetailet.size()-1;i++){
                stringBuffer.append(",(?,?,?)");
            }
            String sql = stringBuffer.toString();
            pst = conn.prepareStatement(sql);
            //批量添加
            for (int i = 0 ;i <orderdetailet.size();i++){
                Integer orderid = orderdetailet.get(i).getOrderId();
                Integer foodid = orderdetailet.get(i).getFoodId();
                Integer quantity = orderdetailet.get(i).getQuantity();
                pst.setInt(1+(i*3),orderid);
                pst.setInt(2+(i*3),foodid);
                pst.setInt(3+(i*3),quantity);
            }
            a = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return a;
    }
//根据orderid查询  返回数据用来向订单表中添加数据
    @Override
    public List<OrderDetailet> query(Integer orderId) throws SQLException {
        List<OrderDetailet> list = new ArrayList<>();
        String sql = "select * from orderdetailet o,food f where o.foodId = f.foodId and orderId = ?";
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1,orderId);
            rs = pst.executeQuery();
            while (rs.next()){
                OrderDetailet orderDetailet = new OrderDetailet();
                orderDetailet.setOdId(rs.getInt("odId"));
                orderDetailet.setOrderId(rs.getInt("orderId"));
                orderDetailet.setFoodId(rs.getInt("foodId"));
                orderDetailet.setQuantity(rs.getInt("quantity"));
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                orderDetailet.setFood(food);
                list.add(orderDetailet);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
